/*******************************************************************************
 * Copyright ©  2011 Hulles Industries LLC <hulles.com>
 * All rights reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *     
 *******************************************************************************/
package com.hulles.sancho.engine.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.hulles.sancho.engine.DbManager;
import com.hulles.sancho.engine.Utils;

/**
 * EntityType replaces old EntityEnum, and maps a list of entities from the database
 * @author hulles
 */
public final class EntityType {
    private final static String LOADSTMT = "SELECT * FROM entity";
    private Map<String, Entity> entities;
    private static EntityType instance = null;

    private EntityType() {
        // singleton, don't instantiate publicly
        // loads from the database the first time it's instantiated privately
        DbManager mgr;
        ResultSet result;
        String key;
        String displayName;
        Boolean isSystemTable;
        String className;
        Entity entity;

        entities = new HashMap<String, Entity>();
        mgr = DbManager.getInstance();
        result = mgr.executeQuery(LOADSTMT);
        if (result != null) {
            try {
                while (result.next()) {
                    key = result.getString("key");
                    displayName = result.getString("display_name");
                    isSystemTable = result.getBoolean("system_table");
                    className = result.getString("class_name");
                    entity = new Entity();
                    entity.setDisplayName(displayName);
                    entity.setIsSystemTable(isSystemTable);
                    entity.setClassName(className);
                    entities.put(key, entity);
                }
            } catch (SQLException ex) {
                Utils.sqlError(ex);
                Utils.exitApplication("Unable to load entities from database, quitting application");
            } finally {
                mgr.closeResult(result);
            }
        }
    }

    /**
     * Return an instance of EntityType.
     * @return Instance of EntityType.
     */
    public static synchronized EntityType getInstance() {
        if (instance == null) {
            instance = new EntityType();
        }
        return instance;
    }

    /**
     * Get the display name of the entity type
     * @param key The entity type key
     * @return The display name
     */
    public String getDisplayName(String key) {
        Entity entity;

        entity = entities.get(key);
        if (entity == null) {
            Utils.error("Unable to find entity from key: " + key);
            return null;
        }
        return entity.getDisplayName();
    }

    /**
     * Returns true if the entity type is a "system table", e.g. a table
     * of month names
     * @param key The entity type key
     * @return True if the entity type is a system table
     */
    public Boolean isSystemTable(String key) {
        Entity entity;

        entity = entities.get(key);
        if (entity == null) {
            Utils.error("Unable to find entity from key:" + key);
            return null;
        }
        return entity.isSystemTable();
    }

    public String getClassName(String key) {
        Entity entity;

        entity = entities.get(key);
        if (entity == null) {
            Utils.error("Unable to find entity from key:" + key);
            return null;
        }
        return entity.getClassName();
    }
    
    private class Entity {
        private String displayName;
        private String className;
        private Boolean isSystemTable;

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public Boolean isSystemTable() {
            return isSystemTable;
        }

        public void setIsSystemTable(Boolean isSystemTable) {
            this.isSystemTable = isSystemTable;
        }

		public String getClassName() {
			return className;
		}

		public void setClassName(String className) {
			this.className = className;
		}

    }
}
